using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

/*<time-entry>
  <id type="integer">#{id}</id>
  <project-id type="integer">#{project-id}</project-id>
  <person-id type="integer">#{person-id}</person-id>
  <date type="date">#{date}</date>
  <hours>#{hours}</hours>
  <description>#{description}</description>
  <todo-item-id type="integer">#{todo-item-id}</todo-item-id>
</time-entry>*/

public class TimeTrack
{
    private int _ID;
    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }

    private int _ProjectID;
    public int ProjectID
    {
        get { return _ProjectID; }
        set { _ProjectID = value; }
    }

    private int _PersonID;
    public int PersonID
    {
        get { return _PersonID; }
        set { _PersonID = value; }
    }

    private DateTime _Date;
    public DateTime Date
    {
        get { return _Date; }
        set { _Date = value; }
    }

    private string _Hours;
    public string Hours
    {
        get { return _Hours; }
        set { _Hours = value; }
    }

    private string _Description;
    public string Description
    {
        get { return _Description; }
        set { _Description = value; }
    }

    private int _TodoItemID;
    public int TodoItemID
    {
        get { return _TodoItemID; }
        set { _TodoItemID = value; }
    }

    public TimeTrack(int id, int projectID, int personID, DateTime date, string hours, string description, int todoItemID)
    {
        _ID = id;
        _ProjectID = projectID;
        _PersonID = personID;
        _Date = date;
        _Hours = hours;
        _Description = description;
        _TodoItemID = todoItemID;
    }

    public static IList<TimeTrack> Parse(XmlNodeList timetrackNodes)
    {
        IList<TimeTrack> timetrack = new List<TimeTrack>();

        foreach (XmlElement node in timetrackNodes)
        {
            int id = int.Parse(node.GetElementsByTagName("id").Item(0).InnerText);
            int projectID = int.Parse(node.GetElementsByTagName("project-id").Item(0).InnerText);
            int personID = int.Parse(node.GetElementsByTagName("person-id").Item(0).InnerText);
            DateTime date = DateTime.Parse(node.GetElementsByTagName("date").Item(0).InnerText);
            string hours = node.GetElementsByTagName("hours").Item(0).InnerText;
            string description = node.GetElementsByTagName("description").Item(0).InnerText;
            int todoItemID = int.Parse(node.GetElementsByTagName("todo-item-id").Item(0).InnerText);

            TimeTrack t = new TimeTrack(id, projectID, personID, date, hours, description, todoItemID);
            timetrack.Add(t);
        }
        return timetrack;
    }
    
  /*<request>
  <entry>
    <project-id>#{project_id}</project-id>
    <person-id>#{person-id}</person-id>
    <date>#{date}</date>
    <hours>#{hours}</hours>

    <!-- if not associated with a todo-item -->
    <description>#{description}</description>

    <!-- if associated with a todo-item -->
    <todo-item-id>#{todo-item-id}</todo-item-id>
  </entry>
</request>*/

    public static string CreateMessageRequest(int projectID, int personID, DateTime date, string hours, string description)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        XmlWriter w = new XmlTextWriter(sw);

        w.WriteStartElement("request");
        w.WriteStartElement("entry");

        w.WriteStartElement("project-id");
        w.WriteString(projectID.ToString());
        w.WriteEndElement(); //end project

        w.WriteStartElement("person-id");
        w.WriteString(personID.ToString());
        w.WriteEndElement(); //end person

        w.WriteStartElement("date");
        w.WriteString(date.ToString());
        w.WriteEndElement(); //end date

        w.WriteStartElement("hours");
        w.WriteString(hours);
        w.WriteEndElement(); //end hours

        //if (TodoItem)
        //{
        //    w.WriteStartElement("todo-item-id");
        //    w.WriteString(todoItemID.ToString());
        //    w.WriteEndElement(); //end todoItemID
        //}
        //else
        //{
        w.WriteStartElement("description");
        w.WriteString(description);
        w.WriteEndElement(); //end description        
        //}
        w.WriteEndElement();
        w.WriteEndElement();

        return sw.ToString();
    }
}

